package ssm.ann;

import org.apache.catalina.*;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.security.SecurityClassLoad;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.modeler.Registry;
import org.apache.tomcat.util.scan.StandardJarScanner;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.nio.file.Paths;
import java.util.Objects;

public class MyTomcat extends Tomcat {
    public static void main(String[] args) throws Exception {
        Registry.disableRegistry();
        SecurityClassLoad.securityClassLoad(Thread.currentThread().getContextClassLoader());

        // 编译后的 class 类路径
        String webappDir = ResourceUtils.getFile(Objects.requireNonNull(MyTomcat.class.getResource("/"))).toString();
        // tomcat 目录，随意
        String tomcatBaseDir = Paths.get(webappDir).getParent().toString() + File.separatorChar + "tomcat";

        MyTomcat myTomcat = new MyTomcat();
        myTomcat.setBaseDir(tomcatBaseDir);
        // myTomcat.init();

        Connector connector = new Connector();
        connector.setPort(8080);
        connector.setURIEncoding("UTF-8");
        myTomcat.getService().addConnector(connector);

        StandardContext ctx = (StandardContext) myTomcat.addWebapp("/", webappDir);
        StandardJarScanner jarScanner = (StandardJarScanner) ctx.getJarScanner();
        jarScanner.setScanManifest(false);

        myTomcat.start();
        myTomcat.getServer().await();
    }

    @Override
    public void initWebappDefaults(String contextPath) {
        Container ctx = getHost().findChild(contextPath);
        initWebappDefaults((Context) ctx);
    }

    public static void initWebappDefaults(Context ctx) {
        // Default servlet
        Wrapper servlet = addServlet(ctx, "default", "org.apache.catalina.servlets.DefaultServlet");
        servlet.setLoadOnStartup(1);
        servlet.setOverridable(true);

        // JSP servlet (by class name - to avoid loading all deps)
        // servlet = addServlet(ctx, "jsp", "org.apache.jasper.servlet.JspServlet");
        // servlet.addInitParameter("fork", "false");
        // servlet.setLoadOnStartup(3);
        // servlet.setOverridable(true);

        // Servlet mappings
        ctx.addServletMappingDecoded("/", "default");
        // ctx.addServletMappingDecoded("*.jsp", "jsp");
        // ctx.addServletMappingDecoded("*.jspx", "jsp");

        // Sessions
        ctx.setSessionTimeout(30);

        // MIME type mappings
        addDefaultMimeTypeMappings(ctx);

        // Welcome files
        ctx.addWelcomeFile("index.html");
        ctx.addWelcomeFile("index.htm");
        // ctx.addWelcomeFile("index.jsp");
    }

    @Override
    public LifecycleListener getDefaultWebXmlListener() {
        return new DefaultWebXmlListener();
    }

    public static class DefaultWebXmlListener implements LifecycleListener {
        @Override
        public void lifecycleEvent(LifecycleEvent event) {
            if (Lifecycle.BEFORE_START_EVENT.equals(event.getType())) {
                initWebappDefaults((Context) event.getLifecycle());
            }
        }
    }
}
